package com.lmxdawn.api.im.handler;

import cn.hutool.core.date.DateUtil;
import com.lmxdawn.api.im.service.ImUtil;
import org.jim.core.ImChannelContext;
import org.jim.core.ImPacket;
import org.jim.core.config.ImConfig;
import org.jim.core.exception.ImException;
import org.jim.core.packets.ChatBody;
import org.jim.core.packets.ChatType;
import org.jim.core.packets.Command;
import org.jim.core.packets.RespBody;
import org.jim.server.ImServerChannelContext;
import org.jim.server.JimServerAPI;
import org.jim.server.command.AbstractCmdHandler;
import org.jim.server.config.ImServerConfig;
import org.jim.server.protocol.ProtocolManager;
import org.jim.server.queue.MsgQueueRunnable;
import org.jim.server.util.ChatKit;

import static org.jim.core.packets.Command.COMMAND_CHAT_REQ;

public class Notify24CmdHandler extends AbstractCmdHandler {
    @Override
    public Command command() {
        return Command.addAndGet("Notify24", 24);
    }

    @Override
    public ImPacket handler(ImPacket packet, ImChannelContext channelContext) throws ImException {
        ImServerChannelContext imServerChannelContext = (ImServerChannelContext)channelContext;
        if (packet.getBody() == null) {
            throw new ImException("body is null");
        }
        ChatBody chatBody = ChatKit.toChatBody(packet.getBody(), channelContext);
        packet.setBody(chatBody.toByte());
        ChatBody resp = ChatBody.newBuilder()
                .chatType(chatBody.getChatType())
                .groupId(chatBody.getGroupId())
                .from(chatBody.getFrom())
                .to(chatBody.getTo())
                .content("我已阅读并知晓：拍前务必核对上下页唯一且不重复，如购买时有重复商品，造成的损失由我承担！")
                .setCmd(COMMAND_CHAT_REQ.getNumber())
                .msgType(chatBody.getMsgType())
                .setCreateTime(DateUtil.current())
                .build();
        ImUtil.sendToGroup(chatBody.getGroupId(), resp);
        return ProtocolManager.Packet.success(channelContext);
    }
}
